import { ref } from 'vue'
import { defineStore } from 'pinia'
import _ from 'lodash'

export const useCommonStore = defineStore('common', () => {
  const breadcrumbsList = ref<commonTypes.menuItem[]>([])
  const currentRoute = ref<commonTypes.menuItem>()
  /**
   * 添加面包屑item
   * @param item
   */
  const addBreadcrumbs = (item: commonTypes.menuItem) => {
    setCurrentRoute(item)
    // 记录当前所有面包屑
    const arr: commonTypes.menuItem[] =
      JSON.parse(sessionStorage.getItem('breadcrumbsList') as string) || []
    arr.push(item)
    const result = _.uniqBy(arr, (item) => item.path)
    sessionStorage.setItem('breadcrumbsList', JSON.stringify(result))
    breadcrumbsList.value = result
  }
  /**
   * 直接设置当前路由
   * @param info
   */
  const setCurrentRoute = (info: commonTypes.menuItem) => {
    currentRoute.value = info
    sessionStorage.setItem('currentRoute', JSON.stringify(info))
  }
  /**
   * 直接设置列表
   * @param info
   */
  const setBreadcrumbsList = (info: commonTypes.menuItem[]) => {
    sessionStorage.setItem('breadcrumbsList', JSON.stringify(info))
    breadcrumbsList.value = info
  }
  return { breadcrumbsList, addBreadcrumbs, currentRoute, setBreadcrumbsList, setCurrentRoute }
})
